from .trigger import ObjectEnterTrigger


# 联动进入报警器
class LinkageEnterTrigger(ObjectEnterTrigger):
    
    def enter(self, obj, objs):
        # 当有新物体进入时检查是否满足联动报警条件
        # 检测联动报警的分类物体是否都存在
        linkage_objs = []
        all_objs_alerted = True
        for linkage_obj_label in self.alert_objs:
            this_kind_objs = []
            for linkage_obj in objs[linkage_obj_label]:
                if linkage_obj.exists:
                    this_kind_objs.append(linkage_obj)
                if not linkage_obj.linkage_alerted:
                    all_objs_alerted = False
            if not this_kind_objs:  # 某个联动的类型不存在任何物体
                return
            linkage_objs.extend(this_kind_objs)
        if all_objs_alerted:  # 所有都物体已经联动报过警
            return
        # 开始画画，标出所有物体
        image = obj.image.copy()
        for o in linkage_objs:
            o.make_shotcut(image)
            o.linkage_alerted = True
        
        # 联动报警
        self.alert_to_cloud({
            'type': '联动报警',
            'target': f'{"和".join(self.alert_objs)}一起出现',
            'action': 1,
            'image': {
                'base64': self.encode_jpg_image(image),
                'ext': 'jpg'
            }
        })
